<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../libs/mui.min.css" rel="stylesheet"/>
    <link href="../libs/app.css" rel="stylesheet"/>
    <link href="../css/main.css" rel="stylesheet"/>
    <script src="../libs/route.js"></script>
    <style type="text/css">
        .active {
            display: none;
        }
    </style>
</head>

<body>
<div class="head mui-bar mui-bar-nav">
    <p id="title" class="mui-title">小马时贷</p>
    <span id="head-address"><img src="../images/loading.gif"></span>
</div>

<div class="page-content"></div>

<div class="foot mui-bar-tab">
    <div style="height: 5px;"></div>
    <a href="#/home">
        <i class="icon-item active"></i>
        <p class="mui-tab-label">首页</p>
    </a>
    <a href="#/join">
        <i class="icon-item"></i>
        <p class="mui-tab-label">加盟接单</p>
    </a>
    <a href="#/recommend">
        <i class="icon-item"></i>
        <p class="mui-tab-label">推荐赚钱</p>
    </a>
    <a href="#/helper">
        <i class="icon-item"></i>
        <p class="mui-tab-label">信贷助手</p>
    </a>
    <a href="#/mine">
        <i class="icon-item"></i>
        <p class="mui-tab-label">我的</p>
    </a>
</div>
<script src="../libs/mui.min.js"></script>
<script src="../js/jquery-3.1.1.min.js"></script>
<script src="../js/url.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=AIEts6CoFGpdivbIz3uI3SrAG9iDB0Ko"></script>
<script src="../js/location.js"></script>
<script>
    $('.mui-bar-tab > a').on('tap', function (event) {
        $(event.currentTarget).addClass('active').siblings().removeClass('active');
    });

    var indexPage = (function () {
        var _indexPage = {};
        var indexPageData = [];
        for (var i = 0; i < 5; i++) {
            indexPageData.push(null);
        }
        _indexPage.indexPageData = indexPageData;
//        _indexPage.audio = function () {
//            if (localStorage.voice == 'true') $('#audio')[0].play();
//            localStorage.setItem('voice', 'false');
//        }
        return _indexPage;
    })();

    spaRouters.map('/home', function (transition) {
        //异步加载js
//        spaRouters.asyncFun('name.js', transition);
        $('#head-address').show(0);
        $('.mui-bar-tab > a').eq(0).addClass('active').siblings().removeClass('active');
        if (!indexPage.indexPageData[0]) {
            //或者同步执行回调
            spaRouters.syncFun(function (transition) {
                $.get('home.html', function (data) {
                    indexPage.indexPageData[0] = $(data);
                    $('.page-content').html(data);
                });
            }, transition);
        } else {
            $('.page-content').html(indexPage.indexPageData[0]);
        }
    });

    spaRouters.map('/join', function (transition) {
        $('#head-address').hide(0);
        if (!indexPage.indexPageData[1]) {
            spaRouters.syncFun(function (transition) {
                $.get('join.html', function (data) {
                    indexPage.indexPageData[1] = $(data);
                    $('.page-content').html(data);
                });
            }, transition);
        } else {
            $('.page-content').html(indexPage.indexPageData[1]);
        }
    });

    spaRouters.map('/recommend', function (transition) {
        $('#head-address').hide(0);
        if (!indexPage.indexPageData[2]) {
            spaRouters.syncFun(function (transition) {
                $.get('recommend.html', function (data) {
                    indexPage.indexPageData[2] = $(data);
                    $('.page-content').html(data);
                });
            }, transition);
        } else {
            $('.page-content').html(indexPage.indexPageData[2]);
        }
    });

    spaRouters.map('/helper', function (transition) {
        $('#head-address').hide(0);
        if (!indexPage.indexPageData[3]) {
            spaRouters.syncFun(function (transition) {
                $.get('helper.html', function (data) {
                    indexPage.indexPageData[3] = $(data);
                    $('.page-content').html(data);
                });
            }, transition);
        } else {
            $('.page-content').html(indexPage.indexPageData[3]);
        }
    });

    spaRouters.map('/mine', function (transition) {
        $('#head-address').hide(0);
        if (!indexPage.indexPageData[4]) {
            spaRouters.syncFun(function (transition) {
                $.get('mine.html', function (data) {
                    indexPage.indexPageData[4] = $(data);
                    $('.page-content').html(data);
                });
            }, transition);
        } else {
            $('.page-content').html(indexPage.indexPageData[4]);
        }
    });

    // 路由初始化
    spaRouters.init();

    var ParamsUrl = spaRouters.getParamsUrl(),
        $tab_a = $('.mui-bar-tab > a');
    if (ParamsUrl.path == '/home') {
        $tab_a.eq(0).addClass('active').siblings().removeClass('active');
    } else if (ParamsUrl.path == '/join') {
        $tab_a.eq(1).addClass('active').siblings().removeClass('active');
    } else if (ParamsUrl.path == '/recommend') {
        $tab_a.eq(2).addClass('active').siblings().removeClass('active');
    } else if (ParamsUrl.path == '/helper') {
        $tab_a.eq(3).addClass('active').siblings().removeClass('active');
    } else if (ParamsUrl.path == '/mine') {
        $tab_a.eq(4).addClass('active').siblings().removeClass('active');
    }

    var getLocation = function () {
        getLocation_bd(function (e) {
            if (e.street == 'undefiend') {
                document.getElementById("head-address").innerHTML = e.district; //通过具体的地址集合获取具体市名称
            }
            document.getElementById("head-address").innerHTML = e.street; //通过具体的地址集合获取具体市名称
            if (document.getElementById("head-address").innerHTML == 'undefined') {
                document.getElementById("head-address").innerHTML = '暂无法获取位置';
            }
        }, function (e) {
            $('#head-address').html('暂无法获取位置');
        });
    }

    $('#head-address').on('tap', function () {
        getLocation();
    });

    $(function () {
        getLocation();

        //自定义事件，模拟点击“首页选项卡”
        function gohome() {
            $('.mui-bar-tab > a:nth-of-type(1)').addClass('active').siblings().removeClass('active');
            $('.mui-bar-tab > a:nth-of-type(1)').trigger('click');
            $('#head-address').show(0);
        }

        window.addEventListener('gohome', function () {
            gohome();
        });

    });
</script>
</body>

</html>